#include "dbhelper.h"
#include <iostream>
#include <cstring>

using std::string;
using std::cerr;
using std::cout;
using std::endl;

dbHelper::dbHelper(const string& dbname) {
    if (sqlite3_open(dbname.data(), &db) != SQLITE_OK) {
        cerr << "无法打开数据库: " << sqlite3_errmsg(db) << endl;
    } else {
        cout << "成功打开数据库: " << dbname << endl;
    }
}

dbHelper::~dbHelper() {
    sqlite3_close(db);
}

bool dbHelper::regist(const string& name, const string& pswd) {
    const char* sql = "INSERT INTO user(name, pswd) VALUES(?, ?)";
    sqlite3_stmt* stmt = nullptr;
    
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
        cerr << "准备注册语句失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 2, pswd.c_str(), -1, SQLITE_TRANSIENT);
    
    int res = sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    
    if (res != SQLITE_DONE) {
        cerr << "注册失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    cout << "用户注册成功: " << name << endl;
    return true;
}

bool dbHelper::login(const string& name, const string& pswd) {
    const char* sql = "SELECT pswd FROM user WHERE name = ?";
    sqlite3_stmt* stmt = nullptr;
    
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
        cerr << "准备登录语句失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_TRANSIENT);
    
    int res = sqlite3_step(stmt);
    if (res == SQLITE_ROW) {
        const char* db_pswd = (const char*)sqlite3_column_text(stmt, 0);
        bool success = (db_pswd != nullptr && pswd == db_pswd);
        sqlite3_finalize(stmt);
        
        cout << "登录尝试: " << name << " - " << (success ? "成功" : "失败") << endl;
        return success;
    }
    
    sqlite3_finalize(stmt);
    cerr << "用户不存在: " << name << endl;
    return false;
}

bool dbHelper::saveSchedule(const string& username, const string& date, const string& event) {
    const char* sql = "INSERT INTO schedules(username, date, event) VALUES(?, ?, ?)";
    sqlite3_stmt* stmt = nullptr;
    
    cout << "尝试保存日程: " << username << ", " << date << ", " << event << endl;
    
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
        cerr << "准备保存日程语句失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    sqlite3_bind_text(stmt, 1, username.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 2, date.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 3, event.c_str(), -1, SQLITE_TRANSIENT);
    
    int res = sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    
    if (res != SQLITE_DONE) {
        cerr << "保存日程失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    cout << "日程保存成功" << endl;
    return true;
}

bool dbHelper::updateSchedule(const string& username, const string& oldDate, 
                            const string& newDate, const string& newEvent) {
    const char* sql = "UPDATE schedules SET date=?, event=? WHERE username=? AND date=?";
    sqlite3_stmt* stmt = nullptr;
    
    cout << "尝试更新日程: " << username << ", " << oldDate << " -> " << newDate << endl;
    
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
        cerr << "准备更新日程语句失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    sqlite3_bind_text(stmt, 1, newDate.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 2, newEvent.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 3, username.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 4, oldDate.c_str(), -1, SQLITE_TRANSIENT);
    
    int res = sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    
    if (res != SQLITE_DONE || sqlite3_changes(db) == 0) {
        cerr << "更新日程失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    cout << "日程更新成功" << endl;
    return true;
}

bool dbHelper::deleteSchedule(const string& username, const string& date) {
    const char* sql = "DELETE FROM schedules WHERE username=? AND date=?";
    sqlite3_stmt* stmt = nullptr;
    
    cout << "尝试删除日程: " << username << ", " << date << endl;
    
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
        cerr << "准备删除日程语句失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    sqlite3_bind_text(stmt, 1, username.c_str(), -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt, 2, date.c_str(), -1, SQLITE_TRANSIENT);
    
    int res = sqlite3_step(stmt);
    sqlite3_finalize(stmt);
    
    if (res != SQLITE_DONE || sqlite3_changes(db) == 0) {
        cerr << "删除日程失败: " << sqlite3_errmsg(db) << endl;
        return false;
    }
    
    cout << "日程删除成功" << endl;
    return true;
}

std::vector<std::pair<std::string, std::string>> 
dbHelper::querySchedules(const std::string& username, const std::string& date) {
    std::vector<std::pair<std::string, std::string>> result;
    sqlite3_stmt* stmt = nullptr;
    
    cout << "执行日程查询: username=" << username;
    if (!date.empty()) {
        cout << ", date=" << date;
    }
    cout << endl;

    const char* sql = date.empty() ? 
        "SELECT date, event FROM schedules WHERE username=? ORDER BY date" :
        "SELECT date, event FROM schedules WHERE username=? AND date=?";
    
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
        cerr << "准备查询语句失败: " << sqlite3_errmsg(db) << endl;
        return result;
    }
    
    sqlite3_bind_text(stmt, 1, username.c_str(), -1, SQLITE_TRANSIENT);
    if (!date.empty()) {
        sqlite3_bind_text(stmt, 2, date.c_str(), -1, SQLITE_TRANSIENT);
    }
    
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const char* sdate = (const char*)sqlite3_column_text(stmt, 0);
        const char* event = (const char*)sqlite3_column_text(stmt, 1);
        
        if (sdate && event) {
            result.emplace_back(sdate, event);
            cout << "查询到日程: " << sdate << " - " << event << endl;
        } else {
            cerr << "查询到空数据，跳过" << endl;
        }
    }
    
    sqlite3_finalize(stmt);
    
    if (result.empty()) {
        cout << "未查询到匹配的日程" << endl;
    } else {
        cout << "共查询到 " << result.size() << " 条日程" << endl;
    }
    
    return result;
}